package br.com.miboco.patterns.template.post;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class PostTemplateComponent extends Composite {

	
	// ------------>>template<<------------------------//
	public PostTemplateComponent() {
		initGrid();
		initVerticalPanel();
		addLabelToGrid();
		addPushButtonToGrid();

		// ------ setup of widget area by subclass-------//
		this.widgetArea = getTypeArea();
		this.widgetArea.setTitle("new post");
		this.grid.setWidget(1, 0, widgetArea);
		// ----------------------------------------------//

		initWidget(this.verticalPanel);
	}

	private void initGrid() {
		this.html = new HTML("blog is empty");
		this.grid = new Grid(MAX_GRID, MAX_GRID);
	}

	/**
	 * Setup of vertical panel
	 **/
	private void initVerticalPanel() {
		this.verticalPanel = new VerticalPanel();
		this.verticalPanel.setSize("100%", "100%");
		this.verticalPanel.setVisible(true);
		this.verticalPanel.add(this.grid);
		this.verticalPanel.add(this.html);
	}

	/**
	 * Setup of add push button
	 **/
	private void addPushButtonToGrid() {
		this.pushButton = new PushButton();
		this.pushButton.setTitle("add a new post");
		this.pushButton.setHTML("+");
		this.pushButton.setSize("09px", "15px");
		this.pushButton.setStyleName("buttonadd");
		this.pushButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				updateLastMessage();
			}
		});
		this.grid.setWidget(1, 1, pushButton);
	}

	protected void updateLastMessage() {
		this.html.setText( getLastMessage() );
	}

	/**
	 * Setup of max typed
	 **/
	private void addLabelToGrid() {
		this.labelMaxtyped = new Label(Integer.toString(MAX_TYPED));
		this.labelMaxtyped.setSize("36px", "17px");
		this.grid.setWidget(0, 1, labelMaxtyped);
	}

	/**
	 * Initialize other components at grid
	 **/
	public abstract Widget getTypeArea();

	public void updateLabelMaxTyped(int newValue) {
		int update = (MAX_TYPED - newValue);
		if (update >= 0){
			labelMaxtyped.setText("" + update);
			change = true;
		}
	}
	
	public boolean isChange(){
		return this.change;
		
	}
	
	
	public String getLastMessage() {
		return lastMessage;
	}

	public void setLastMessage(String lastMessage) {
		this.lastMessage = "last message: "+lastMessage;
	}

	// -------------instance variables ----------//
	private static final int MAX_TYPED = 120;
	private static final int MAX_GRID = 2;

	private Widget widgetArea;
	private PushButton pushButton;
	private Label labelMaxtyped;
	private VerticalPanel verticalPanel;
	private Grid grid;
	private HTML html;
	private String lastMessage;
	private boolean change;
}
